<?php

namespace api\lib;

class Str
{
    const CHS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz~`!@#$%^&*()_+-=";

    public static function smsCode($len = 6)
    {
        return self::rand($len, "0123456789");
    }

    public static function randKey()
    {
        return self::rand(12, self::CHS);
    }

    public static function randAlpha($len = 4, $chs = "")
    {
        if (empty($chs)) {
            $chs = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
        }
        return self::rand($len, $chs);
    }

    /**
     * @param int $len
     * @return string
     */
    private static function rand($len, $s)
    {
        $n = strlen($s) - 1;
        $r = "";
        while ($len-- > 0) {
            $r .= $s[rand(0, $n)];
        }
        return $r;
    }

    public static function isValidMobile($s)
    {
        $matches = array();
        preg_match("/^1[3456789][0-9]{9}$/", $s, $matches);
        return !empty($matches);
    }

    private static function isLeapYear(/* int */ $yyyy)
    {
        return ($yyyy % 400 == 0) || ($yyyy % 4 == 0 && $yyyy % 100 != 0);
    }

    public static function prevDate(/* string */$today) {
        list($yyyy, $mm, $dd) = explode('-', $today);

        $yearMonth = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
        if (self::isLeapYear($yyyy)) {
            $yearMonth[2] += 1;
        }
        // prev day
        $dd -= 1;
        if ($dd < 1) {
            $mm -= 1;
            $dd = $yearMonth[$mm];
        }
        if ($mm < 1) {
            $yyyy -= 1;
            $mm = 12;
            $dd = $yearMonth[$mm];
        }
        return sprintf("%04d-%02d-%02d", $yyyy, $mm, $dd);
    }

    public static function nextDate(/* string */ $today)
    {
        list($yyyy, $mm, $dd) = explode('-', $today);

        $yearMonth = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
        if (self::isLeapYear($yyyy)) {
            $yearMonth[2] += 1;
        }
        // next day
        $dd += 1;
        if ($dd > $yearMonth[$mm]) {
            $mm += 1;
            $dd = 1;
        }
        if ($mm > 12) {
            $yyyy += 1;
            $mm = 1;
        }
        return sprintf("%04d-%02d-%02d", $yyyy, $mm, $dd);
    }

    public static function isValidDateTime(/* string */ $s)
    {
        $date = substr($s, 0, 10);
        if (!preg_match("/^\d{4}-\d{2}-\d{2}$/", $date)) {
            return false;
        }
        list($date, $time) = explode(' ', $s);
        list($yyyy, $mm, $dd) = explode('-', $date);
        list($hh, $min, $sec) = explode(':', $time);
        $year = intval($yyyy);
        if ($year < 1970 || $year > 9999) {
            return false;
        }
        $month = intval($mm);
        if ($month < 1 || $month > 12) {
            return false;
        }
        $day = intval($dd);
        $yearMonth = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
        if (self::isLeapYear($year)) {
            $yearMonth[2] += 1;
        }
        if ($day < 1 || $day > $yearMonth[$month]) {
            return false;
        }
        if (!is_numeric($min)) {
            return false;
        }
        if ($min < 0 || $min >= 60) {
            return false;
        }
        if (!is_numeric($sec)) {
            return false;
        }
        if ($sec < 0 || $sec >= 60) {
            return false;
        }
        return true;
    }

}